/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.entities;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author A7med
 */
@Entity
@Table(name = "department")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Department.findAll", query = "SELECT d FROM Department d"),
    @NamedQuery(name = "Department.findById", query = "SELECT d FROM Department d WHERE d.id = :id"),
    @NamedQuery(name = "Department.findByName", query = "SELECT d FROM Department d WHERE d.name = :name"),
    @NamedQuery(name = "Department.findByDesc", query = "SELECT d FROM Department d WHERE d.desc = :desc"),
    @NamedQuery(name = "Department.findAllChildren", query = "SELECT d FROM Department d WHERE d.parent = :parent")})
public class Department implements Serializable {

    @Basic(optional = true)
    private Integer parent;
    @Basic(optional = false)
    @NotNull
    @Column(name = "read_access")
    private short readAccessValue;
    @Basic(optional = false)
    @NotNull
    @Column(name = "write_access")
    private short writeAccessValue;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 64)
    @Column(name = "name")
    private String name;
    
    @Size(min = 1, max = 255)
    @Column(name = "description")
    private String desc;
    @ManyToMany()
    @JoinTable(name = "department_permission", joinColumns = {
        @JoinColumn(name = "accessor_id", referencedColumnName = "id")},
            inverseJoinColumns = {
                @JoinColumn(name = "accessed_id", referencedColumnName = "id"),
                @JoinColumn(name = "type_of_access", referencedColumnName = "read_access")})
    private Set<Department> readAccess;
    @ManyToMany()
    @JoinTable(name = "department_permission", joinColumns = {
        @JoinColumn(name = "accessor_id", referencedColumnName = "id")},
            inverseJoinColumns = {
                @JoinColumn(name = "accessed_id", referencedColumnName = "id"),
                @JoinColumn(name = "type_of_access", referencedColumnName = "write_access")})
    private Set<Department> writeAccess;
    @OneToMany(mappedBy = "department")
    private List<Users> usersList;

    public List<Users> getUsersList() {
        return usersList;
    }

    public void setUsersList(List<Users> usersList) {
        this.usersList = usersList;
    }

    public Set<Department> getReadAccess() {
        return readAccess;
    }

    public void setReadAccess(Set<Department> readAccess) {
        this.readAccess = readAccess;
    }

    public Set<Department> getWriteAccess() {
        return writeAccess;
    }

    public void setWriteAccess(Set<Department> writeAccess) {
        this.writeAccess = writeAccess;
    }

    public Department() {
    }

    public Department(Integer id) {
        this.id = id;
    }

    public Department(Integer id, String name, String desc) {
        this.id = id;
        this.name = name;
        this.desc = desc;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Department)) {
            return false;
        }
        Department other = (Department) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name;
    }

    public Integer getParent() {
        return parent;
    }

    public void setParent(Integer parent) {
        this.parent = parent;
    }

}
